package com.qf.springmvc02.controller;

import com.github.pagehelper.PageInfo;
import com.qf.springmvc02.entity.User;
import com.qf.springmvc02.response.Response;
import com.qf.springmvc02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vue")
public class VueUserController {
    @Autowired
    UserService userService;


     //分页查询员工列表
     //@param pageIndex 第几页
     //@param pageSize  每页几条数据
     //@return 分页对象
    @RequestMapping("/users")
    public Response<PageInfo<User>> getAllUser(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.selectAllUser(pageIndex, pageSize);
        return Response.success(pageInfo);
    }

     //根据ID删除用户
     // @param id 用户ID
     //@return 响应对象
     @GetMapping("/user/{id}")
     public Response<User> deleteById(@PathVariable("id") Integer id) {
         userService.deleteById(id);
         return Response.success();
     }


     //根据ID查询单个用户
     //@param id 用户ID
     //@return 响应对象

    @GetMapping("/getUser/{id}")
    public Response<User> selectById(@PathVariable("id") Integer id) {
        return Response.success(userService.selectById(id));
    }

     //编辑用户
     //@param user 用户
     //@return 响应对象
    @PostMapping("/user")
    public Response<Integer> updateUser(User user) {
        return Response.success(userService.update(user));
    }

    //新增用户
    //@param user 用户
    //@return 响应对象
    @PostMapping("/userAdd")
    public Response<Integer> insertUser(User user) {
        return Response.success(userService.addUser(user));
    }
}
